import { debounce } from '@/utils'
import moment from 'moment'
export default {
    data() {
        return {
            isLoading: true,
            echartInstance: null,
            echartId: '',
            echartData: {},
            echartsWidth: 0,
            echartsHeight: 0,
            axisColor: '#D0D0D0',
            axisColorw: '#8FA4B4',
            xLineColor: '#6A9BFD',
            tooltipBorderColor: '#6AD6FD',
            tooltipBgColor: 'rgba(37, 35, 32, 0.3)'
        }
    },
    computed: {
        fontSize: function() {
            return this.$store.getters.fontSize
        }
    },
    mounted() {
        this.initchart()
        this._resizeHandler = debounce(() => {
            if (this.echartInstance) {
                this.setOptions() // 更改字体大小等
                this.echartInstance.resize() // 更改范围大小
            }
        }, 100)
        window.addEventListener('resize', this._resizeHandler)
    },
    beforeDestroy() {
        if (!this.echartInstance) {
            return
        }
        window.removeEventListener('resize', this._resizeHandler)
        this.echartInstance.dispose()
        this.echartInstance = null
    },
    methods: {
        initchart() {
            const div = document.getElementById(this.echartId)
            this.echartsWidth = div.clientWidth
            this.echartsHeight = div.clientHeight
            if (this.echartInstance) {
                this.echartInstance.dispose()
                this.echartInstance = null
            }
            this.echartInstance = this.$echarts.init(div)
        },
        formatterTime(value) {
            var dateTimer = this.$moment(value).format('MM-DD')
            if (dateTimer === 'Invalid date') dateTimer = '-'
            return dateTimer
        },
        getDaysX(startTime, endTime) {
            var dateArr = []
            var st = moment(moment(startTime).format('YYYY-MM-DD'))
            var et = moment(moment(endTime).format('YYYY-MM-DD'))
            while (st.isBefore(et)) {
                dateArr.push(st.format('MM-DD'))
                st = st.add(1, 'd')
            }
            dateArr.push(et.format('MM-DD'))
            return (dateArr)
        },
        getXName(arr) {
            return arr.map(item => {
                return this.formatterTime(item)
            })
        }
    }
}

